/******************************************************************************
* number of movers by origin-destination cz
*****************************************************************************/



*****************************************************************************
*** inputs for maps of origin vs destination moves
*** use the baseline delta at zip code level
*****************************************************************************	

use asofdate move_t num_mv numobs cz_o cz_d delta_unpdcol_cust min_rel_q max_rel_q using "${dataRoot}/tu_mover_delta_zipnodraw.dta", clear

*restrict to one observation per mover
tab num_mv
sum asofdate
keep if asofdate==`r(min)' & num_mv==1
	
* in-sample movers
qui keep if min_rel_q<=-${evtQ} & max_rel_q>=${evtQ} & max_rel_q!=.	
			
*movers who moved prior to 2008
keep if move_t<200800
		
*keep movers with matched non-movers 
keep if delta_unpdcol_cust!=.

*check total number of valid movers
sum numobs
return list
	

*collapse at origin X destination level
collapse (sum) numobs, by(cz_o cz_d)

compress
save "${outDataDir}/mover_cz_od__zipnodraw.dta", replace



	
********************************************************************************************************************
**  Maps of movers (Figure A8)
********************************************************************************************************************

*set up globals based on geo level
geoLevel

use "${outDataDir}/mover_cz_od__zipnodraw", clear
rename cz_o o
rename cz_d d

global vlist "o d"
foreach v in $vlist{
	preserve
		rename `v' cz
		collapse (sum) numobs, by(cz)
		rename numobs mvr_`v'
		
		merge 1:1 cz using  "${dataRoot}/tu_acs_${Geo}_${sumStatsyr}"
		sum mvr_`v'
		sum numobs
		
		*maps of origina/destination CZ movers
		replace mvr_`v'=0 if mvr_`v'==.
		mapPlot mvr_`v' ${mapGeo}  "mover_`v'_${Geo}_${sumStatsyr}" "" 0
		
		*fraction over TU number of observation
		gen mvr_frac = 100*mvr_`v'/numobs
		mapPlot mvr_frac ${mapGeo}  "frac_mover_`v'" "" 1
	restore
}

*TU number of observation
use "${dataRoot}/tu_acs_${Geo}_${sumStatsyr}", clear
replace numobs = numobs/1000
mapPlot numobs ${mapGeo}  "tu_numobs_${Geo}_${sumStatsyr}" "" 0	
	
		


	